import java.util.Scanner;
import java.util.InputMismatchException;

public class Statistics {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一段英文文本：");

        try {
            String text = scanner.nextLine();
            int wordCount = countWords(text);
            System.out.println("单词数量: " + wordCount);
        } catch (InputMismatchException e) {
            System.out.println("输入错误：请输入有效的文本。");
        } catch (Exception e) {
            System.out.println("发生未知错误： " + e.getMessage());
        } finally {
            scanner.close();
        }
    }

    public static int countWords(String text) {
        if (text == null || text.isEmpty()) {
            return 0;
        }
        // 使用正则表达式匹配单词边界，并排除标点符号
        String[] words = text.split("\\W+");
        return words.length;
    }
}
